package cn.passlink.bletoothchat.mFragment;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import androidx.fragment.app.Fragment;
import androidx.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import cn.passlink.bletoothchat.Adapter.FriendsAdapter;
import cn.passlink.bletoothchat.Entity.Friends;
import cn.passlink.bletoothchat.R;
import cn.passlink.bletoothchat.SqlTables.FriendsTableManager;
import cn.passlink.bletoothchat.SqlTables.MyDatabaseHelper;

public class FriendsScrollingFragment extends Fragment {

    private Friends mFriends = new Friends();
    private SQLiteDatabase database;
    private MyDatabaseHelper databaseHelper;

    private View rootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_friends_scrolling, container, false);
        // 初始化 databaseHelper
        databaseHelper = new MyDatabaseHelper(requireContext(), 2);
        return rootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        // 找到 ListView 对象
        ListView listView = view.findViewById(R.id.show_friends_listview);

        // 创建 FriendsAdapter 对象
        List<Friends> friendsList = new ArrayList<Friends>();

        database=databaseHelper.getWritableDatabase();
        FriendsTableManager mFriendsTableManager=new FriendsTableManager(database);
        FriendsAdapter adapter = new FriendsAdapter(getActivity(), mFriendsTableManager.getAllFriends());

        // 设置适配器
        listView.setAdapter(adapter);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        // 关闭数据库连接
        if (database != null && database.isOpen()) {
            database.close();
        }
    }
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        // 在此处关联上下文对象
    }
    @Override
    public void onDetach() {
        super.onDetach();


    }



}